/**
 * 
 */
package com.vision.core.cm.db.data;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import net.sf.gilead.pojo.gwt.LightEntity;

/**
 * @author Mark
 *
 */
@Entity
@Table(name = Company.TABLE)
public class Company extends LightEntity implements Serializable {
	
	public static interface FieldSpecs extends Serializable {
		int CODE_MAX = 10;
		int NAME_MAX = 100;
		int ADDRESS_MAX = 300;
		int PHONE_MAX = 100;
		int FAX_MAX = 100;
	}

	private static final long serialVersionUID = -2125728821847184395L;
	
	public static final String TABLE = "company";
	public static final String COL_ID = "id";
	public static final String COL_CODE = "code";
	public static final String COL_NAME = "name";
	public static final String COL_ADDRESS = "address";
	public static final String COL_PHONE = "phone";
	public static final String COL_FAX = "fax";

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "scompany")
	@SequenceGenerator(name = "scompany", sequenceName = "company_seq")
	@Column(name = COL_ID)
	private Long id;
	
	@Column(name = COL_CODE, nullable = false, unique = true, updatable = false, length = FieldSpecs.CODE_MAX)
	private String code;
	
	@Column(name = COL_NAME, nullable = false, length = FieldSpecs.NAME_MAX)
	private String name;
	
	@Column(name = COL_ADDRESS, nullable = true, length = FieldSpecs.ADDRESS_MAX)
	private String address;
	
	@Column(name = COL_PHONE, nullable = true, length = FieldSpecs.PHONE_MAX)
	private String phone;
	
	@Column(name = COL_FAX, nullable = true, length = FieldSpecs.FAX_MAX)
	private String fax;

	@Column(name = "status", nullable = false)
	@Enumerated(EnumType.ORDINAL)
	private CompanyStatus status;

	@OneToMany(mappedBy = "company", cascade = { CascadeType.PERSIST, CascadeType.MERGE })
	private Set<CompanyGroup> companyGroups = new HashSet<CompanyGroup>();
	
	public Company() { }
	
	public Company(Long id) {
		this(id, null, null);
	}
	
	public Company(Long id, String code, String name) {
		this.id = id;
		this.code = code;
		this.name = name;
	}
	
	@Override
	public boolean equals(Object object) {
		if (object instanceof Company) {
			Company c = (Company) object;
			return getId().equals(c.getId());
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		return getId() == null ? super.hashCode() : getId().hashCode();
	}
	
	@Override
	public String toString() {
		return id + ": " + name + " (" + code + ")";
	}

	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getFax() {
		return fax;
	}

	public void setFax(String fax) {
		this.fax = fax;
	}
	
	public CompanyStatus getStatus() {
		return status;
	}
	
	public void setStatus(CompanyStatus status) {
		this.status = status;
	}
	
	public Set<CompanyGroup> getCompanyGroups() {
		return companyGroups;
	}

	public void setCompanyGroups(Set<CompanyGroup> companyGroups) {
		this.companyGroups = companyGroups;
	}

	
	// Inner Classes ***********************************************************


	public enum CompanyStatus {
		// NOTE When adding new fields, add at the bottom
		ACTIVE,
		DEACTIVATED,
	}

}
